package com.tnovoselec.android.travelassistant.fragment;

import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.tnovoselec.android.common.async.Async;
import com.tnovoselec.android.common.async.listener.Outcome;
import com.tnovoselec.android.common.async.notif.NotifierFactory;
import com.tnovoselec.android.common.util.ApplicationUtil;
import com.tnovoselec.android.travelassistant.Config;
import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.adapter.ExchangeRatesAdapter;
import com.tnovoselec.android.travelassistant.async.DownloadExchangeRates;
import com.tnovoselec.android.travelassistant.dto.CurrencyDto;
import com.tnovoselec.android.travelassistant.dto.CurrencyResultHolder;

public class ExchangeRatesFragment extends SherlockFragment {

	private ListView list;
	private TextView pubDate;
	private ExchangeRatesAdapter adapter;
	private Handler handler;
	private ProgressDialog progressDialog;

	private List<CurrencyDto> rates;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		adapter = new ExchangeRatesAdapter(getActivity());
		handler = new Handler();
		if (ApplicationUtil.isOnline(getActivity())) {
			getRates();
		}else {
			Log.e("Kita", "Kita od neta!");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.exchange_rates, container, false);

		list = (ListView) v.findViewById(R.id.exchange_rates_list);
		pubDate = (TextView) v.findViewById(R.id.exchange_rates_pubdate);
		list.setAdapter(adapter);
		return v;
	}

	private void getRates() {
		progressDialog = ProgressDialog.show(getActivity(), getString(R.string.meteo_dialog_title), getString(R.string.meteo_dialog_message), true, false);

		Async.doAsync(null, new DownloadExchangeRates(Config.EXCHANGE_RATES_URL, getActivity(), false), new Outcome<Void, Boolean>() {

			@Override
			protected void succeeded(Void key, Boolean result) {
				pubDate.setText(getString(R.string.valid_since) + CurrencyResultHolder.dateOfPublish);
				rates = CurrencyResultHolder.getAll();
				adapter.setData(rates);
				if (progressDialog != null && progressDialog.isShowing()) {
					progressDialog.dismiss();
				}
			}

			@Override
			protected void failed(Void key, Throwable t) {
				if (progressDialog != null && progressDialog.isShowing()) {
					progressDialog.dismiss();
				}
				Log.e("Kita!", t.getMessage());
				t.printStackTrace();

			}
		}, NotifierFactory.newThreadSpecificNotifier(handler));
	}

}
